package it.polimi.sartori_daniello_picca.meteocal.boundary;

import it.polimi.sartori_daniello_picca.meteocal.entity.Calendar;
import it.polimi.sartori_daniello_picca.meteocal.entity.Event;
import it.polimi.sartori_daniello_picca.meteocal.entity.Notification;
import it.polimi.sartori_daniello_picca.meteocal.entity.Users;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Ale
 */
@Stateless
public class NotificationManager {
    
    @PersistenceContext
    EntityManager em;
    
    public void create(Notification entity) {
        em.persist(entity);
    }
    
    public void edit(Notification entity) {
        em.merge(entity);
    }
    
    public void remove(Notification entity) {
        em.remove(em.merge(entity));
    }
    
   public List<Notification> findNotificationByReceiver(Users receiver){
        List<Notification> result= em.createNamedQuery(Notification.findByReceiver, Notification.class)
              .setParameter("receiver", receiver).getResultList();
      return result;
   }
   
   public void notifinvite(List<Users> invited, Event event) {
        Notification invitevent;
        if(invited == null)
            return;
        
        for(Users userinvited: invited){
            invitevent = new Notification();
            invitevent.setNotificationType(Notification.NotificationType.INVITATION);
            invitevent.setReceiver(userinvited);
            invitevent.setEventAssociated(event);
            this.create(invitevent);
            
        }
    }
   
   public void notifmodify(Event event, List<Calendar> cal) {
        Notification modifevent = new Notification();
        if(event == null || cal == null){
            return;
        }
        Users participant;
        for (Calendar c : cal){
            participant = em.createNamedQuery(Users.findByUsername, Users.class).setParameter("username", c.getUsername().getUsername()).getSingleResult();
           modifevent.setEventAssociated(event);
           modifevent.setNotificationType(Notification.NotificationType.MODIFICATION);
           modifevent.setReceiver(participant);
           this.create(modifevent);
        }
    }
   
    public void notif1daybefore(Event event, List<Calendar> list1) {
        Notification badweather1 = new Notification();
        if(list1 == null || event == null){
            return;
        }
        Users participant;
        for (Calendar c : list1){
           participant = em.createNamedQuery(Users.findByEmail, Users.class).setParameter("username", c.getUsername().getUsername()).getSingleResult();
            
           badweather1.setEventAssociated(event);
           badweather1.setNotificationType(Notification.NotificationType.WEATHER1DAY);
           badweather1.setReceiver(participant);
           this.create(badweather1);
        }
    }
    
     public void notif3daysbefore(Event event, Date firstsunnyday) {
        Notification badweather3 = new Notification();
        badweather3.setEventAssociated(event);
        badweather3.setReceiver(event.getCreator());
        badweather3.setNotificationType(Notification.NotificationType.WEATHER3DAY);
        badweather3.setFirstGoodDay(firstsunnyday);
        this.create(badweather3);
    }
}
